package edu.rmit.client;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * This class is to initialize the client GUI program.
 * 
 * @author Thaw Zin
 * @version 1.0
 */
@SuppressWarnings("serial")
public class ClientGUI extends JFrame {
	/**
	 * The server port which provide the RMI service.
	 */
	private String port = "1099";

	/**
	 * The server address which provide the RMI service.
	 */
	private String hostName = "127.0.0.1";

	/**
	 * The window initialize height.
	 */
	private static int HEIGHT = 200;

	/**
	 * The window initialize width.
	 */
	private static int WIDTH = 400;

	/**
	 * The client instance which provide the service.
	 */
	private Client client = null;

	/**
	 * The label which display the server address.
	 */
	private JLabel serverAddressLabel; 

	/**
	 * The label which display the server port address.
	 */
	private JLabel serverPortLabel;

	/**
	 * The text field which display the server address.
	 */
	private JTextField serverAddressField;

	/**
	 * The text field which display the server port.
	 */
	private JTextField serverPortField;

	/**
	 * The button which clicked can connect with an server.
	 */
	private JButton connectServerButton;

	/**
	 * The button which clicked will exit the program.
	 */
	private JButton exitButton;
	
	/**
	 * The JFrame which display the system window.
	 */
	private JFrame frame;

	/**
	 * The constructor to initialize the control. 
	 */
	public ClientGUI() {
		frame = new JFrame("Client Window");
		
		JPanel contentPanel = new JPanel();
		contentPanel.setLayout(new GridLayout(3, 1));

		serverAddressLabel = new JLabel("Host:");
		serverPortLabel = new JLabel("Port:");

		serverAddressField = new JTextField(hostName, 10);
		serverPortField = new JTextField(port, 4);

		connectServerButton = new JButton("Start");
		exitButton = new JButton("Exit");
		connectServerButton
				.addActionListener(new ConnectServerButtonListener());
		exitButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int chooseOption = JOptionPane.showConfirmDialog(null,
						"Are you sure to exit the client?",
						"Your will like to close it!",
						JOptionPane.YES_NO_OPTION);
				if (chooseOption == JOptionPane.YES_OPTION) {
					System.exit(0);
				}
			}
		});
		
		JPanel hostPanel = new JPanel();
		hostPanel.add(serverAddressLabel);
		hostPanel.add(serverAddressField);
		contentPanel.add(hostPanel);

		JPanel portPanel = new JPanel();

		portPanel.add(serverPortLabel);
		portPanel.add(serverPortField);
		contentPanel.add(portPanel);

		JPanel commandPanel = new JPanel();
		commandPanel.add(connectServerButton);
		commandPanel.add(exitButton);
		contentPanel.add(commandPanel);
		
		frame.setContentPane(contentPanel);
		initFrame();
	}
	
	/**
	 * This method is to initialize the frame window.
	 */
	private void initFrame(){
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setVisible(true);
		frame.setSize(WIDTH, HEIGHT);

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = frame.getSize();
		if (frameSize.height > screenSize.height) {
			frameSize.height = screenSize.height;
		}
		if (frameSize.width > screenSize.width) {
			frameSize.width = screenSize.width;
		}
		frame.setLocation((screenSize.width - frameSize.width) / 2,
				(screenSize.height - frameSize.height) / 2);
	}

	/**
	 * This class is to deal with when user clicked the connect to server button.
	 * @author Joey George
	 * @version 1.0
	 *
	 */
	class ConnectServerButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent event) {
			String serverName = serverAddressField.getText();
			String serverPort = serverPortField.getText();

			client = new Client();

			if (!hostName.trim().equals("")) {
				client.setServerName(serverName);
			}
			if (!serverPort.trim().equals("")) {
				int port = 1099;
				try {
					port = Integer.parseInt(serverPort);
				} catch (NumberFormatException e) {
					e.printStackTrace();
				}
				client.setPort(port);
			}
			client.lookUpService();
			frame.dispose();
			new WelcomePanel(client);
		}
	}

	public static void main(String[] args) {
		new ClientGUI();
	}
}
